package net.avh4.scratch.rpg.battle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Battle {
	
	private final List<Character> teamA = new ArrayList<Character>();
	private final List<Character> teamB = new ArrayList<Character>();
	
	private final Queue<Character> nextTurn = new LinkedList<Character>();
	
	private transient HashMap<Character, List<Character>> opponentsMap;
	
	public Battle(List<Character> teamA, List<Character> teamB) {
		this.teamA.addAll(teamA);
		this.teamB.addAll(teamB);
		nextTurn.addAll(teamA);
		nextTurn.addAll(teamB);
		opponentsMap = new HashMap<Character, List<Character>>();
		for (Iterator<Character> it = teamA.iterator(); it.hasNext();) {
			List<Character> ops = new ArrayList<Character>(teamB);
			opponentsMap.put(it.next(), ops);
		}
		for (Iterator<Character> it = teamB.iterator(); it.hasNext();) {
			List<Character> ops = new ArrayList<Character>(teamA);
			opponentsMap.put(it.next(), ops);
		}
	}
	
	public List<Character> getTeamA() {
		return new ArrayList<Character>(teamA);
	}
	
	public List<Character> getTeamB() {
		return new ArrayList<Character>(teamB);
	}

	public boolean hasEnded() {
		return nextTurn.isEmpty();
	}

	public void next() {
		if (nextTurn.isEmpty()) return;
		
		Character c = nextTurn.poll();
		assert c != null;
		Action a = c.getNextAction(this);
		a.process();
		nextTurn.add(c);
	}

	public List<Character> getOpponents(Character character) {
		return opponentsMap.get(character);
	}

}
